package com.jsb.laundry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsb.laundry.common.exception.BusinessException;
import com.jsb.laundry.common.exception.ErrorCode;
import com.jsb.laundry.dao.JsbPostCommentsLikesMapper;
import com.jsb.laundry.model.entity.JsbPostComments;
import com.jsb.laundry.model.entity.JsbPostCommentsLikes;
import com.jsb.laundry.model.entity.JsbPostLikes;
import com.jsb.laundry.model.entity.JsbPosts;
import com.jsb.laundry.service.JsbPostCommentsLikesService;
import com.jsb.laundry.service.JsbPostCommentsService;
import com.jsb.laundry.service.JsbPostLikesService;
import com.jsb.laundry.service.JsbPostsService;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author ching
* @description 针对表【jsb_post_comments_likes(用户对帖子评论点赞的关系表)】的数据库操作Service实现
* @createDate 2023-08-15 11:52:11
*/
@Service
public class JsbPostCommentsLikesServiceImpl extends ServiceImpl<JsbPostCommentsLikesMapper, JsbPostCommentsLikes>
    implements JsbPostCommentsLikesService{
    @Resource
    private JsbPostCommentsService postCommentsService;
    @Override
    public int doPostCommentLike(Integer commentId, String userId) {
        // 判断实体是否存在，根据类别获取实体
        JsbPostComments postComments = postCommentsService.getById(commentId);
        if (postComments == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已点赞
        // 每个用户串行点赞
        // 锁必须要包裹住事务方法
        JsbPostCommentsLikesService commentLikeService = (JsbPostCommentsLikesService) AopContext.currentProxy();
        synchronized (userId.intern()) {
            return commentLikeService.doPostCommentLikeInner(commentId, userId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int doPostCommentLikeInner(Integer commentId, String userId) {
        JsbPostCommentsLikes postCommentLike = new JsbPostCommentsLikes();
        postCommentLike.setUserId(userId);
        postCommentLike.setCommentId(commentId);
        QueryWrapper<JsbPostCommentsLikes> likeQueryWrapper = new QueryWrapper<>(postCommentLike);
        JsbPostCommentsLikes oldPostCommentLikeThumb = this.getOne(likeQueryWrapper);
        boolean result;
        // 已点赞
        if (oldPostCommentLikeThumb != null) {
            result = this.remove(likeQueryWrapper);
            if (result) {
                // 点赞数 - 1
                result = postCommentsService.update()
                        .eq("id", commentId)
                        .gt("likes", 0)
                        .setSql("likes = likes - 1")
                        .update();
                return result ? -1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未点赞
            result = this.save(postCommentLike);
            if (result) {
                // 点赞数 + 1
                result = postCommentsService.update()
                        .eq("id", commentId)
                        .setSql("likes = likes + 1")
                        .update();
                return result ? 1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }
}




